package com.hmz.backend.service.Impl.chat;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmz.backend.entity.ChatRoom;
import com.hmz.backend.entity.User;
import com.hmz.backend.mapper.ChatRoomMapper;
import com.hmz.backend.service.chat.ChatRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ChatRoomServcieImpl implements ChatRoomService {
    @Autowired
    private ChatRoomMapper chatRoomMapper;
    //创建聊天室
    @Override
    public JSONObject createChatRoom(JSONObject data) {
        //创建数据库
        ChatRoom chatRoom = new ChatRoom();
        chatRoom.setNum(1);
        chatRoom.setTime(new Date());
        //chatRoom.setPerson("user"+data.get("userId"));
        chatRoom.setChatRoomName(data.get("chatRoomName").toString());
        chatRoom.setAvatar("https://ts1.cn.mm.bing.net/th?id=OIP-C.8eJyHGkITYHkBhzdsXpObAHaHK&w=254&h=245&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2");
        try {
            chatRoomMapper.insert(chatRoom); //创建聊天室
            chatRoomMapper.joinChatRoom((Integer) data.get("userId"),chatRoom.getId()); //加入聊天室
            //动态建表
            chatRoomMapper.createMessageTable(chatRoom.getId());
        } catch (Exception e) {
            System.out.println(e);
            return new JSONObject().set("message","创建聊天室失败");
        }
        return new JSONObject().set("message","创建聊天室成功");
    }
    //获取我的聊天室列表
    @Override
    public JSONObject getMyChatRoomList(int userId) {
        List<ChatRoom> myChatRoomList = chatRoomMapper.getMyChatRoomList(userId);
        return new JSONObject().set("message","查询成功!").set("list",myChatRoomList);
    }
    //根据用户输入模糊搜索聊天室
    @Override
    public JSONObject searchChatRoom(String chatRoomName,int userId) {
        List<ChatRoom> chatRooms = chatRoomMapper.searchChatRoom(chatRoomName,userId);
        return new JSONObject().set("message","查询成功").set("list",chatRooms);
    }
    //加入聊天室
    @Override
    public JSONObject joinChatRoom(JSONObject data) {
        int chatRoomId = (int) data.get("chatRoomId");
        int userId = (int) data.get("userId");
        chatRoomMapper.joinChatRoom(userId,chatRoomId);//加入聊天室
        chatRoomMapper.updateChatRoomPersonNum(chatRoomId,"+1");//将指定聊天室的人数+1
        return new JSONObject().set("message","加入聊天室成功");
    }
    //获取聊天室用户列表
    @Override
    public JSONObject getChatRoomUserList(int chatRoomId) {
        List<User> chatRoomUserList = chatRoomMapper.getChatRoomUserList(chatRoomId);
        return new JSONObject().set("message","查询成功").set("list",chatRoomUserList);
    }

    @Override
    public JSONObject deleteMychatRoom(JSONObject data) {
        int userId = (int) data.get("userId");
        int chatRoomId = (int) data.get("chatRoomId");
        //删除加入关系
        chatRoomMapper.deletePerson(userId,chatRoomId);
        chatRoomMapper.updateChatRoomPersonNum(chatRoomId,"-1");// 聊天室人数减一
        QueryWrapper<ChatRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",chatRoomId);
        ChatRoom chatRoom = chatRoomMapper.selectOne(queryWrapper);
        //如果聊天室没有人了，就删表
        if(chatRoom.getNum() == 0){
            chatRoomMapper.deleteMessageTable(chatRoomId);
            chatRoomMapper.deleteById(chatRoom); //去聊天室表中删除对应实体
        }

        return new JSONObject().set("message","删除成功");
    }

}
